from django.contrib.auth.models import User

from users.models import models
from django.utils import timezone
import markdown
from django.utils.html import strip_tags
from django.utils.text import slugify
from markdown.extensions.toc import TocExtension


class Category(models.Model):
    cname = models.CharField(max_length=30, unique=True, verbose_name='类别名称', )

    class Meta:
        db_table = 't_category'
        verbose_name_plural = '类别'

    def __str__(self):
        return f'Category: {self.cname}'


# Create your models here.
class Article(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='ID')
    title = models.CharField(max_length=200, verbose_name="文章标题")  # 设置文章标题的可读性名称
    content = models.fields.TextField(verbose_name="文章内容")
    # body = models.TextField()# 设置文章内容的可读性名称
    # body_html = models.TextField(blank=True, editable=False)
    # created = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 设置创建时间的可读性名称
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    # updated = models.DateTimeField(auto_now=True, verbose_name="修改时间")      # 设置修改时间的可读性名称
    # Category = models.CharField(max_length=100, verbose_name="分类名称", choices=(
    #     ('新闻', '新闻'), ('娱乐', '娱乐'), ('科技', '科技'), ('生活', '生活'), ('军事', '军事')))  # 设置分类名称的可读性名称
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="分类")
    Tag = models.CharField(max_length=100, verbose_name="标签名称",
                           choices=(('python', 'python'), ('django', 'django'), ('java', 'java')))  # 设置标签名称的可读性名称
    author = models.CharField(max_length=100, verbose_name="作者")  # 设置作者的可读性名称
    excerpt = models.CharField(default=0, max_length=200, verbose_name="文章摘要")
    views = models.PositiveIntegerField('阅读次数', default=0, editable=False)
    body_html = models.fields.TextField(blank=True, editable=True, null=True)
    toc_html = models.fields.TextField(blank=True, editable=True, null=True)

    def save(self, *args, **kwargs):
        # TODO: 自动生成摘要
        # 1. 把md的文本内容转换成html的字符串
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
            'markdown.extensions.toc',
            TocExtension(slugify=slugify)
        ])
        self.body_html = md.convert(self.content)
        self.toc_html = md.toc
        # 2. 把html的字符串中html标签通过strip_tags给剔除掉
        # 3. 提取出前54个字符串作为摘要的内容
        self.excerpt = strip_tags(self.body_html)[:54]
        super().save(*args, **kwargs)

    @property
    def comment_count(self):
        return self.comments.count()
    def __str__(self):
        return self.title


class Comment(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='comments')
    name = models.CharField(max_length=100, verbose_name="姓名")
    email = models.EmailField(verbose_name="邮箱")
    content = models.TextField(verbose_name="评论内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="发表时间")
    comment_count = models.IntegerField(default=0)
    like_count = models.IntegerField(default=0)  # 点赞数

    def __str__(self):
        return self.content[:50]

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    avatar = models.ImageField(upload_to='avatar/%Y%m%d/', default='avatar/default.png', verbose_name='头像')


    def __str__(self):
        return self.user.username
class Reply(models.Model):
    content=  models.CharField(max_length=150)
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)




